package com.kanshu.ksgb.fastread.doudou.module.message.presenter;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.MessageBean;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils;
import com.kanshu.ksgb.fastread.doudou.module.message.manager.MessageDao;

import java.util.List;

/**
 * 消息分页器
 * page 从0开始
 * Created by wcy on 2017\6\26 0026.
 */
public class MessagePageLoader {

    public static final int DEFAULT_NUM = 20;

    /**
     * 获取总共页数
     * @param num
     * @return
     */
    public int getTotalpage(int num) {
        MessageDao dao = new MessageDao(UserUtils.getUserId(Xutils.getContext()));
        int messageCount = dao.getMessageCount();
        int totalPage = messageCount / num;
        if (messageCount > 0 && messageCount % num > 0) {
            totalPage++;
        }
        return totalPage;
    }

    public int getTotalpage() {
        return getTotalpage(DEFAULT_NUM);
    }

    /**
     * page 从0开始 获取数据
     * @param page
     * @param num
     * @return
     */
    public List<MessageBean> loadPage(int page, int num) {
        MessageDao dao = new MessageDao(UserUtils.getUserId(Xutils.getContext()));
        int offset = page * num;
        return dao.queryByLimitOffset(num, offset);
    }

    public List<MessageBean> loadPage(int page) {
        return loadPage(page, DEFAULT_NUM);
    }

}
